package com.wanfajie.smsdistribute.ui.template_picker

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.OrientationHelper
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.wanfajie.smsdistribute.R
import com.wanfajie.smsdistribute.common.adapter.CheckedRecyclerAdapter
import com.wanfajie.smsdistribute.template.SmsTemplate
import com.wanfajie.smsdistribute.db.bean.template.TemplateBean
import com.wanfajie.smsdistribute.db.helper.TemplateDatabaseHelper
import kotlinx.android.synthetic.main.activity_template_picker.*

class TemplatePickerActivity : AppCompatActivity() {

    companion object {
        const val EXTRA_KEY_TEMPLATE = "template"
        const val EXTRA_KEY_TEMPLATE_ID = "template_id"
        const val EXTRA_KEY_TEMPLATE_NAME = "template_name"
    }

    private val mAdapter by lazy {
        object : CheckedRecyclerAdapter<TemplateBean>(this, emptyList(),
                R.layout.list_item_template, R.id.checkbox) {
            init {
                setItemCheckable(false)
                setOnLongClickListener { true }
                setOnItemClickListener { _, it ->
                    val template = SmsTemplate.Builder(it).create()

                    val result = Intent()
                    result.putExtra(EXTRA_KEY_TEMPLATE, template)
                    result.putExtra(EXTRA_KEY_TEMPLATE_ID, it.id)
                    result.putExtra(EXTRA_KEY_TEMPLATE_NAME, it.title)
                    setResult(Activity.RESULT_OK, result)
                    finish()
                }
            }
            override fun processViewHolder(p0: CheckedViewHolder, p1: TemplateBean) {
                p0.get<TextView>(R.id.template_title).text = p1.title
                p0.get<TextView>(R.id.template_content).text = p1.content
                p0.get<Button>(R.id.btn_edit).visibility = View.GONE
                p0.get<Button>(R.id.btn_delete).visibility = View.GONE
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_template_picker)

        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)

        loadTemplates()
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = OrientationHelper.VERTICAL
        template_list.layoutManager = layoutManager
        template_list.adapter = mAdapter
    }

    private fun loadTemplates() {
        val dao = TemplateDatabaseHelper.getDao(TemplateBean::class.java)
        dao?.let {
            val beans = it.queryForAll()
            beans.sortByDescending { tp ->
                tp.updateTimestamp
            }
            mAdapter.setItems(beans)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item?.itemId) {
            android.R.id.home ->  {
                finish()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}